package com.wenyang.controller;


//import com.github.pagehelper.PageInfo;
//import com.wenyang.entity.TabRoute;
import com.wenyang.dao.RouteDao;
import com.wenyang.entity.TabRoute;
import com.wenyang.entity.VO.TabRouteVo;
import com.wenyang.service.RouteImgService;
import com.wenyang.service.RouteService;
import com.wenyang.utils.FileUploadUtil;
import com.wenyang.utils.R;
import com.wenyang.utils.ScpClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/route")
@Api(value = "旅游路线",tags = {"旅游路线"})
public class RouteController {

    @Autowired
    private RouteService routeService;

    @Autowired
    private RouteImgService routeImgService;

    @Autowired
    private RouteDao routeDao;

    @GetMapping("/findAll")
    @ApiOperation("旅游路线列表")
    public R findAll(){

        return R.success(routeService.findAll(),"执行成功");
    }


    @PostMapping("/listByPage")
    @ApiOperation("分页查询旅游路线列表")
    public Map<String,Object> listByPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam(required = false) String rname){
//        if (pageNum == 0){
//            pageNum = 1;
//        }
//        if (pageSize == 0) {
//            pageSize = 10;
//        }if (null == rname || rname.length() == 0){
//            rname = null;
//        }
        int startIdx = (pageNum - 1) * pageSize, size = pageSize;
        List<TabRoute> routeList = routeService.listByPage(startIdx,size,rname);
        int total = routeDao.selectPageWithParam(rname);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",routeList);
        map.put("total",total);
        map.put("msg","查询成功");
        return map;
    }


    @PostMapping("/upload")
    @ResponseBody
    @ApiOperation("上传图片")
    public R uploadImage(@RequestParam("image") MultipartFile file){

        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tGRMSvwDkdsV4PJs2Kg";
        String accessKeySecret = "2SnSYZwBUvkZh5e9u2DZ2yEjuCETTI";
        String bucketName = "20240416-wenyangfile";
        String bucImagePath = "img/product/small/";
        FileUploadUtil uploadUtil = new FileUploadUtil(endpoint, accessKeyId, accessKeySecret, bucketName);
        try {
            String fileExtension = "";
            String contentType = file.getContentType();
            if (contentType.equals("image/jpeg")) {
                fileExtension = "jpg";
            } else if (contentType.equals("image/png")) {
                fileExtension = "png";
            } else if (contentType.equals("image/gif")) {
                fileExtension = "gif";
            } else {
                // 默认返回jpg作为扩展名
                fileExtension = "jpg";
            }
            int dotIndex = file.getOriginalFilename().indexOf(".");
            String fileName = file.getOriginalFilename().substring(0, dotIndex);
            // 创建临时文件并保存上传文件
            File tempFile = File.createTempFile(UUID.randomUUID().toString(),"."+fileExtension);
            file.transferTo(tempFile);
            String objectKey = tempFile.getName();
            String ossPathAndName = bucImagePath + objectKey;
            uploadUtil.FileUploadUtil(tempFile,ossPathAndName);
            return R.success("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("文件转换出现问题");
        }
    }

    @GetMapping("/findAllRouteImg")
    @ApiOperation("查询所有旅游路线相关的图片")
    public R findAllRouteImg(){
        return R.success(routeImgService.findAllRouteImg());
    }

    @PostMapping("/findRouteImgById")
    @ApiOperation("根据id查询旅游路线图片")
    @ResponseBody
    public R findRouteImgById(@RequestParam int rid){
        return R.success(routeImgService.findRouteImgById(rid));
    }

    @PostMapping(value = "/saveRoute", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation("添加旅游路线")
    @ResponseBody
    public R saveRoute(@RequestBody TabRouteVo tabRouteVo){
        return R.success(routeService.saveRoute(tabRouteVo));
    }


    @PostMapping("/removeRoute")
    @ApiOperation("删除旅游路线")
    public R removeRoute(@RequestParam String ids){
        return R.success(routeService.removeRoute(ids));
    }
}
